<?php
/**
 * Created by PhpStorm.
 * User: hao
 * Date: 2018/10/26
 * Time: 5:48 PM
 */

namespace app\api\model;


class Product extends BaseModel
{
    protected $hidden = ['delete_time','main_img_id','pivot','from','','create_time','update_time'];

    public function getMainImgUrlAttr($value,$data) {
        return $this->prefixImgUrl($value,$data);
    }

    public static function getMostRecent($count) {
        $product = self::limit($count)->order('create_time desc')->select();
        return collection($product);
    }

    public static function getProductsByCategoryID($categoryID) {
        $products = self::where('category_id','=',$categoryID)->select();
        return collection($products);
    }

    public function imgs() {
        return $this->hasMany('ProductImage','product_id','id');
    }

    public function properties() {
        return $this->hasMany('ProductProperty','product_id','id');
    }

    public static function getProductDetail($id) {
        $product = self::with([
            'imgs'=>function($quary){
                $quary->with('img')
                ->order('order','desc');
            }
        ])
            ->with(['properties'])
            ->find($id);
        return $product;
    }

}